package com.ltu.m7011e.imagine.sudoku.client;

import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public abstract class Game
{
	protected HorizontalPanel gameContainer = new HorizontalPanel();
	
	protected Label timeLabel = new Label("00:00");
	protected Label attemptsLabel = new Label("Attempts: 0");
	protected Label successesLabel = new Label("Successes: 0");
	protected Label failedLabel = new Label("Failed: 0");
	
	protected int attempts = 0;
	protected int successes = 0;
	protected int failed = 0;
	
	private int time = 0;
	
	public Game()
	{
		timer();
		
		RootPanel.get("content").clear();
		RootPanel.get("content").add(gameContainer);
	}
	
	private void timer()
	{
		Timer t = new Timer() {
			public void run() {
				time++;
				timeLabel.setText(getTime());
			}
		};
		
		// Schedule the timer to run once in 5 seconds.
		t.scheduleRepeating(1000);
	}
	
	protected void updateAttempts()
	{
		attemptsLabel.setText("Attempts: " + attempts);
	}
	
	protected void updateFailed()
	{
		failedLabel.setText("Failed: " + failed);
	}
	
	protected void updateSuccesses()
	{
		successesLabel.setText("Successes: " + successes);
	}
	
	private String getTime()
	{
		String minutesString;
		String secondsString;
		
		int minutes = time / 60;
		int seconds = time % 60;
		
		if(minutes < 10)
		{
			minutesString = "0" + minutes;
		}
		else
		{
			minutesString = "" + minutes;
		}
		
		if(seconds < 10)
		{
			secondsString = "0" + seconds;
		}
		else
		{
			secondsString = "" + seconds;
		}
		
		return minutesString + ":" + secondsString;
	}
}
